package fragment.listfragments;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import entity.ListPageEntity;
import fragment.AbsBaseFragment;
import fragment.PopularProgramPageFragment;
import volley.VolleySingleton;

/**
 * 榜单 fragment
 * Created by dllo on 15/11/17.
 */
public class ListPageFragment extends AbsBaseFragment implements View.OnClickListener {
//    private RecyclerView recyclerView;
//    private ListPageAdapter adapter;
    private String refreshUrl = "http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=and" +
            "-a1&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.26.2";
    private ImageLoader imageLoader;
    private NetworkImageView listItemFocusImg,jmListPageItemImgo, jmListPageItemImgt, jmListPageItemImgth;
    private TextView ListPageTitle, listItemTitleo, listItemTitlet, listItemTitleth,
            listItemContento, listItemContentt, listItemContentth;
    private NetworkImageView TjmListPageItemImgo, TjmListPageItemImgt, TjmListPageItemImgth;
    private TextView TListPageTitle, TlistItemTitleo, TlistItemTitlet, TlistItemTitleth,
            TlistItemContento, TlistItemContentt, TlistItemContentth;

    private LinearLayout listClickLLO,listClickLLT,listClickLLTh,listClickLLFo,listClickLLFi,listClickLLSi;
    @Override
    protected int getLayoutView() {
        return R.layout.fragment_page_list;
    }

    @Override
    protected void initView() {
        //recyclerView = findView(R.id.listRecyclerView);
        listItemFocusImg = findView(R.id.listItemFocusImg);
        ListPageTitle = findView(R.id.ListPageTitle);
        jmListPageItemImgo = findView(R.id.jmListPageItemImgo);
        jmListPageItemImgt = findView(R.id.jmListPageItemImgt);
        jmListPageItemImgth = findView(R.id.jmListPageItemImgth);
        listItemTitleo = findView(R.id.listItemTitleo);
        listItemTitlet = findView(R.id.listItemTitlet);
        listItemTitleth = findView(R.id.listItemTitleth);
        listItemContento = findView(R.id.listItemContento);
        listItemContentt = findView(R.id.listItemContentt);
        listItemContentth = findView(R.id.listItemContentth);
        TListPageTitle = findView(R.id.TListPageTitle);
        TjmListPageItemImgo = findView(R.id.TjmListPageItemImgo);
        TjmListPageItemImgt = findView(R.id.TjmListPageItemImgt);
        TjmListPageItemImgth = findView(R.id.TjmListPageItemImgth);
        TlistItemTitleo = findView(R.id.TlistItemTitleo);
        TlistItemTitlet = findView(R.id.TlistItemTitlet);
        TlistItemTitleth = findView(R.id.TlistItemTitleth);
        TlistItemContento = findView(R.id.TlistItemContento);
        TlistItemContentt = findView(R.id.TlistItemContentt);
        TlistItemContentth = findView(R.id.TlistItemContentth);

        listClickLLO = findView(R.id.listClickLLO);
        listClickLLT = findView(R.id.listClickLLT);
        listClickLLTh = findView(R.id.listClickLLTh);
        listClickLLFo = findView(R.id.listClickLLFo);
        listClickLLFi = findView(R.id.listClickLLFi);
        listClickLLSi = findView(R.id.listClickLLSi);
    }

    @Override
    protected void initData() {
        listClickLLO.setOnClickListener(this);
        listClickLLT.setOnClickListener(this);
        listClickLLTh.setOnClickListener(this);
        listClickLLFo.setOnClickListener(this);
        listClickLLFi.setOnClickListener(this);
        listClickLLSi.setOnClickListener(this);
        imageLoader = VolleySingleton.getInstance().getImageLoader();
//        adapter = new ListPageAdapter(getContext());
//        GridLayoutManager manager = new GridLayoutManager(getContext(), 1);
//        manager.setOrientation(LinearLayout.VERTICAL);
//        recyclerView.setLayoutManager(manager);
//        recyclerView.setAdapter(adapter);
        initNetwork();
    }


    private void initNetwork() {
        VolleySingleton.getInstance().addRequest(refreshUrl,
                ListPageEntity.class, new Response.Listener<ListPageEntity>() {
                    @Override
                    public void onResponse(ListPageEntity response) {
                        if (response != null) {
                          //  adapter.addData(response);
                            listItemFocusImg.setImageUrl(response.getFocusImages().getList().get(0).getPic(), imageLoader);
                            ListPageTitle.setText(response.getDatas().get(0).getTitle());
                            jmListPageItemImgo.setImageUrl(response.getDatas().get(0).getList().get(0).getCoverPath(), imageLoader);
                            jmListPageItemImgt.setImageUrl(response.getDatas().get(0).getList().get(1).getCoverPath(),imageLoader);
                            jmListPageItemImgth.setImageUrl(response.getDatas().get(0).getList().get(2).getCoverPath(), imageLoader);
                            listItemTitleo.setText(response.getDatas().get(0).getList().get(0).getTitle());
                            listItemTitlet.setText(response.getDatas().get(0).getList().get(1).getTitle());
                            listItemTitleth.setText(response.getDatas().get(0).getList().get(2).getTitle());
                            listItemContento.setText(response.getDatas().get(0).getList().get(0).getFirstTitle());
                            listItemContentt.setText(response.getDatas().get(0).getList().get(1).getFirstTitle());
                            listItemContentth.setText(response.getDatas().get(0).getList().get(2).getFirstTitle());
                            TListPageTitle.setText(response.getDatas().get(1).getTitle());
                            TjmListPageItemImgo.setImageUrl(response.getDatas().get(1).getList().get(0).getCoverPath(), imageLoader);
                            TjmListPageItemImgt.setImageUrl(response.getDatas().get(1).getList().get(1).getCoverPath(),imageLoader);
                            TjmListPageItemImgth.setImageUrl(response.getDatas().get(1).getList().get(2).getCoverPath(), imageLoader);
                            TlistItemTitleo.setText(response.getDatas().get(1).getList().get(0).getTitle());
                            TlistItemTitlet.setText(response.getDatas().get(1).getList().get(1).getTitle());
                            TlistItemTitleth.setText(response.getDatas().get(1).getList().get(2).getTitle());
                            TlistItemContento.setText(response.getDatas().get(1).getList().get(0).getFirstTitle());
                            TlistItemContentt.setText(response.getDatas().get(1).getList().get(1).getFirstTitle());
                            TlistItemContentth.setText(response.getDatas().get(1).getList().get(2).getFirstTitle());
                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }
    String URLCLASSIC = "http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=ranking%3" +
            "Aalbum%3Asubscribed%3A30%3A0&pageId=1&pageSize=40";
    String URLranking="http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=android&key=" +
            "ranking%3Aalbum%3Acustom%3A42&pageId=1&pageSize=40";

    String URLGrl="http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/anchor?device=android&" +
        "key=ranking%3Aanchor%3Acustom%3A43&pageId=1&pageSize=40";

    String URLBOY="http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/anchor?device=android&key=" +
            "ranking%3Aanchor%3Acustom%3A44&pageId=1&pageSize=40";
    String URLNEWS="http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/anchor?device=android&key=" +
            "ranking%3Aanchor%3Acustom%3A45&pageId=1&pageSize=40";
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.listClickLLO:
                PopularProgramPageFragment p = new PopularProgramPageFragment();
                goToNext(p);
                break;
            case R.id.listClickLLT:
               ListclassicFragment classicfragment =
                       ListclassicFragment.newInstance(URLCLASSIC,"最多收藏排行榜");
                goToNext(classicfragment);

                Toast.makeText(getContext(), "2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.listClickLLTh://有声小说风云榜
                ListclassicFragment rankfragment =
                        ListclassicFragment.newInstance(URLranking,"有声小说风云榜");
                goToNext(rankfragment);
                Toast.makeText(getContext(), "3", Toast.LENGTH_SHORT).show();
                break;
            case R.id.listClickLLFo://天生尤物女神榜
                ListAnchorFragment anchorFragment =
                        ListAnchorFragment.newInstance(URLGrl,"天生尤物女神榜");
                  goToNext(anchorFragment);
                Toast.makeText(getContext(), "4", Toast.LENGTH_SHORT).show();
                break;
            case R.id.listClickLLFi://优声优色男神榜
                ListAnchorFragment manFragment =
                        ListAnchorFragment.newInstance(URLBOY,"优声优色男神榜");
                goToNext(manFragment);

                Toast.makeText(getContext(), "5", Toast.LENGTH_SHORT).show();
                break;
            case R.id.listClickLLSi://后声可谓新人榜
                ListAnchorFragment newFragment =
                        ListAnchorFragment.newInstance(URLNEWS,"后声可谓新人榜");
                goToNext(newFragment);
                Toast.makeText(getContext(), "6", Toast.LENGTH_SHORT).show();
                break;

        }
    }
}
